package com.ganzalang.gmall.command.inventory.updater;

import com.ganzalang.gmall.command.api.dto.SubOrderVariantDTO;
import com.ganzalang.gmall.command.constants.InventoryLogConstants;
import com.ganzalang.gmall.command.domain.ProductVariantInventory;
import com.ganzalang.gmall.command.domain.ProductVariantInventoryLog;
import com.ganzalang.gmall.command.enums.InventoryLogEventEnum;
import com.ganzalang.gmall.command.service.ProductVariantInventoryService;
import org.springframework.context.ApplicationEventPublisher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: 扣减锁定库存
 * @author: Ganzalang
 * @Date 2021/3/13 11:26
 */
public class DeductionLockInventoryUpdater extends AbstractInventoryUpdater{
    /**
     * 用户下单对应商品的DTO对象
     */
    private Map<Long,SubOrderVariantDTO> subOrderVariantDTOMap;

    /**
     * 构造方法
     * @param inventoryService 商品库存service组件
     * @param productVariantInventories 商品库存DO对象
     * @param subOrderVariantDTOMap 用户下单对应商品的DTO对象Map,key:skuId,value:对应的数量
     */
    public DeductionLockInventoryUpdater(
            ProductVariantInventoryService inventoryService,
            List<ProductVariantInventory> productVariantInventories,
            Map<Long,SubOrderVariantDTO> subOrderVariantDTOMap,
            ApplicationEventPublisher publisher,
            Long shopId) {
        super(inventoryService, productVariantInventories, publisher, shopId);
        this.subOrderVariantDTOMap = subOrderVariantDTOMap;
    }

    /**
     * 更新可用库存
     */
    @Override
    protected void updateAvailableInventory() {
    }

    /**
     * 更新锁定库存
     */
    @Override
    protected void updateLockInventory() {
        for (ProductVariantInventory inventory : productVariantInventories) {
            SubOrderVariantDTO subOrderVariantDTO = subOrderVariantDTOMap.get(inventory.getSkuId());
            for (ProductVariantInventory productVariantInventory : productVariantInventories) {
                productVariantInventory.setLockedStockQuantity(-subOrderVariantDTO.getQuantity());
            }
        }
    }

    /**
     * 更新库存日志表
     */
    @Override
    protected void updateInventoryLog() {
        productVariantInventoryLogs = new ArrayList<>(productVariantInventories.size());
        for (ProductVariantInventory inventory : productVariantInventories) {
            SubOrderVariantDTO subOrderVariantDTO = subOrderVariantDTOMap.get(inventory.getSkuId());
            ProductVariantInventoryLog log = new ProductVariantInventoryLog();
            log.setSpuId(inventory.getSpuId());
            log.setSkuId(subOrderVariantDTO.getSkuId());
            log.setChangStockQuantity(0);
            log.setAvailableStockQuantity(inventory.getAvailableStockQuantity());
            productVariantInventoryLogs.add(log);
        }
    }

    /**
     * 回滚操作
     */
    @Override
    protected void rollback() {

    }

    @Override
    public Boolean updateSkusInventory() {
        return null;
    }
}
